﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emergency4.Session;

namespace Emergency4.Dal
{
    internal class AdminDal
    {
        static Session2Entities en = new Session2Entities();
        internal static List<EmergencyMaintenance> findAllEmergency()
        {
            return en.EmergencyMaintenances.OrderByDescending(o => o.PriorityID ).ThenBy(o => o.EMReportDate).ToList();
        }

        internal static EmergencyMaintenance findByEmergencyId(int id)
        {
            return en.EmergencyMaintenances.Where(o => o.ID == id).FirstOrDefault();
        }

        internal static List<Part> findAllPartName()
        {
            return en.Parts.ToList();
        }
        internal static List<ChangedPart> findAllChangePartById(int id)
        {
            return en.ChangedParts.Where(o => o.ID == id).ToList();
        }

        internal static void updateEmergencyMaintenance(EmergencyMaintenance emergencyMaintenance)
        {

            try
            {
                en.Entry(emergencyMaintenance).State = System.Data.Entity.EntityState.Modified;
            }
            catch (Exception ex)
            {

            }

            en.SaveChanges();
        }

        internal static void removeEmergencyMaintenance(int assetId)
        {
            //Session2Entities en = new Session2Entities();
            List<ChangedPart> list = en.ChangedParts.Where(o => o.EmergencyMaintenanceID == assetId).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                en.ChangedParts.Remove(list[i]);
            }
            en.SaveChanges();
        }
        internal static void insertPart(ChangedPart part)
        {
            try
            {
                en.ChangedParts.Add(part);
            }
            catch (Exception ex)
            {

            }

            en.SaveChanges();
        }

        internal static long findAllChangePart(string v)
        {

            return en.ChangedParts.Where(o => o.Part.Name == v).FirstOrDefault().ID;
        }
    }
}
